﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;

namespace QLKS.DAL
{
    public class ThamSoHeThongDAO
    {
        private SqlDataProvider dataProvider;

        public ThamSoHeThongDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<ThamSoHeThong> GetByAll(ThamSoHeThong obj)
        {
            List<ThamSoHeThong> lstData = new List<ThamSoHeThong>();;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_ThamSoHeThong_GetByAll"
                    , new string[] { "MaThamSoHeThong", "TenThamSoHeThong", "GiaTri", "ApDung", "GhiChu" }
                    , new object[] { obj.MaThamSoHeThong, obj.TenThamSoHeThong, obj.GiaTri, obj.ApDung, obj.GhiChu }
                );
                ThamSoHeThong entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new ThamSoHeThong();
                    entity.MaThamSoHeThong = Convert.ToInt32(myrow["MaThamSoHeThong"]);
                    entity.TenThamSoHeThong = myrow["TenThamSoHeThong"] is DBNull ? string.Empty : myrow["TenThamSoHeThong"].ToString();
                    entity.GiaTri = myrow["GiaTri"] is DBNull ? string.Empty : myrow["GiaTri"].ToString();
                    entity.ApDung = myrow["ApDung"] is DBNull ? false : Convert.ToBoolean(myrow["ApDung"]);
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(ThamSoHeThong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_ThamSoHeThong_Insert"
                    , new string[] { "MaThamSoHeThong", "TenThamSoHeThong", "GiaTri", "ApDung", "GhiChu" }
                    , new object[] { obj.MaThamSoHeThong, obj.TenThamSoHeThong, obj.GiaTri, obj.ApDung, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Update(ThamSoHeThong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_ThamSoHeThong_Update"
                        , new string[] { "MaThamSoHeThong", "TenThamSoHeThong", "GiaTri", "ApDung", "GhiChu" }
                        , new object[] { obj.MaThamSoHeThong, obj.TenThamSoHeThong, obj.GiaTri, obj.ApDung, obj.GhiChu }
                    );
            return i == 1 ? true : false;
        }

        public bool Delete(ThamSoHeThong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_ThamSoHeThong_Delete"
                        , new string[] { "MaThamSoHeThong", "TenThamSoHeThong", "GiaTri", "ApDung", "GhiChu" }
                        , new object[] { obj.MaThamSoHeThong, obj.TenThamSoHeThong, obj.GiaTri, obj.ApDung, obj.GhiChu }
                    );
            return i == 1 ? true : false;
        }
    }
}
